<?php

namespace App\Model\System;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class AuthPermission extends Model
{
    public $table ='auth_permissions';

    public $timestamps = false;

    public $primaryKey = 'id';

    protected $fillable = [
        'type',
        'title',
        'name',
        'src',
    ];

    static public  function getPermissionsByUserID($userid)
    {
        if($userid==1){
            $permissions =  AuthPermission::all();
        }else{
            # 获取 role
            $roleids = [];
            $roles = AuthUserRole::select('roleid')->where('userid', $userid)->get()->toarray();
            foreach ($roles as $role){
                $roleids [] = $role['roleid'];
            }

            # 获取 权限
            $pids = [];
            $perms = AuthRolePermission::select('permissionid')->wherein('roleid', $roleids)->get()->toarray();
            foreach ($perms as $perm){
                $pids[] = $perm['permissionid'];
            }
            $permissions = AuthPermission::wherein('id', $pids)->get();
        }
        return $permissions;
    }

}
